<?php

/*
	Class: Communitas_Integrate_CatalogController

	About: Author
		Rich Joslin

	About: License
		<http://communit.as/docs/license>
*/
class Communitas_Integrate_CatalogController extends Mage_Core_Controller_Front_Action {

    protected $_ignoredAttributeTypes = array();

    protected $_ignoredAttributeCodes = array('entity_id', 'attribute_set_id', 'entity_type_id');

	/* Group: Actions */

	/*
		Function: product
	*/
	public function productAction() {
		$request = $this->getRequest();
		$product_id = $request->getParam('id');
		$store = null; // $request->getParam('store');
		$attributes = $request->getParam('attributes');
		if (trim($attributes) != '') {
			$attributes = unserialize($attributes);
		}
		$product = Mage::getModel('catalog/product');
		if (is_string($product_id)) {
			$idBySku = $product->getIdBySku($product_id);
			if ($idBySku) {
				$product_id = $idBySku;
			}
		}
		if ($store !== null) {
			$product->setStoreId($this->_getStoreId($store));
		}
		$product->load($product_id);
		if (!$product->getId()) {
			die('Product not found.');
		}
		$result = array(
			'product_id' => $product->getId(),
			'sku'        => $product->getSku(),
			'set'        => $product->getAttributeSetId(),
			'type'       => $product->getTypeId(),
			'categories' => $product->getCategoryIds(),
			'websites'   => $product->getWebsiteIds()
		);
		$result['comparable_attributes'] = array();
		foreach ($product->getTypeInstance()->getEditableAttributes() as $attribute) {
			if ($this->_isAllowedAttribute($attribute, $attributes)) {
				$result[$attribute->getAttributeCode()] = $product->getData($attribute->getAttributeCode());
				if ($attribute->getIsComparable() == 1) {
					$result['comparable_attributes'][] = $attribute->getAttributeCode();
				}
			}
		}
		die(serialize($result));
	}

	public function attributesAction() {
		$request = $this->getRequest();
		$set_id = $request->getParam('set_id');
		$attributes = Mage::getModel('catalog/product')->getResource()
			->loadAllAttributes()
			->getSortedAttributes($setId);
		$result = array();
		foreach ($attributes as $attribute) {
			/* @var $attribute Mage_Catalog_Model_Resource_Eav_Attribute */
			if ((!$attribute->getId() || $attribute->isInSet($setId)) && $this->_isAllowedAttribute($attribute)) {
				if (!$attribute->getId() || $attribute->isScopeGlobal()) {
					$scope = 'global';
				} elseif ($attribute->isScopeWebsite()) {
					$scope = 'website';
				} else {
					$scope = 'store';
				}
				$result[] = array(
					'attribute_id' 		=> $attribute->getId(),
					'label'				=> $attribute->getFrontendLabel(),
					'code'		 		=> $attribute->getAttributeCode(),
					'type'		 		=> $attribute->getFrontendInput(),
					'required'	 		=> $attribute->getIsRequired(),
					'apply_to'	 		=> $attribute->getApplyTo(),
					'scope'				=> $scope,
					'is_visible'		=> $attribute->getIsVisible(),
					'is_configurable'	=> $attribute->getIsConfigurable(),
					'is_filterable'		=> $attribute->getIsFilterable(),
					'grouping'			=> $attribute->getAttributeSetInfo()
				);
			}
		}
		die(serialize($result));
	}

	/*
		Function: _isAllowedAttribute
	*/
	protected function _isAllowedAttribute($attribute, $attributes = null) {
		if (is_array($attributes) && !( in_array($attribute->getAttributeCode(), $attributes) || in_array($attribute->getAttributeId(), $attributes))) {
			return false;
		}
		return !in_array($attribute->getFrontendInput(), $this->_ignoredAttributeTypes) && !in_array($attribute->getAttributeCode(), $this->_ignoredAttributeCodes);
	}

}
